package com.xiaoyu.commonlib.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CheckFormatUtils {
    public final static String PHONE_PATTERN = "^1[3,4,5,6,7,8][0-9]{9}$";//手机号
    public final static String PASSWORD_PATTERN = "^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{6,12}$"; //只能是数字和字母混合

    /**
     * 手机号验证
     *
     * @param phone
     * @return NOT_NULL 不能为空，不能是空格
     * FORMAT_JUDGE 格式的判断
     * CORRECT 输入正确
     */
    public static int checkMobilePhone(String phone) {
        if (TextUtils.isEmpty(phone) || phone.equals(" ")) {
            return 1;
        }
        if (phone.length() != 11 || !checkPhone(phone, PHONE_PATTERN)) {
            return 2;
        }
        return 0;
    }

    public static boolean checkPhone(String phone, String pattern) {
        return check(phone, pattern);
    }

    // 判断格式是否正确
    private static boolean check(String strInput, String pattern) {
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(strInput);
        return m.find();
    }

    /**
     * 判断密码格式（只能是数字与字母组合并且6-20字符）
     *
     * @param password
     * @return
     */
    public static boolean checkPassword(String password) {
        return check(password, PASSWORD_PATTERN);
    }


    /**
     * 获取系统星期
     */
    public static String getWeeks() {
        long time = System.currentTimeMillis();
        Date date = new Date(time);
        SimpleDateFormat format = new SimpleDateFormat("EEEE");
        return format.format(date);
    }
    /**
     * 获取系统当前年月日（yyyy-MM-dd）
     */
    public static String getCurrentDate() {
//        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
        String date = format.format(new Date());
        return date;
    }
    public static boolean isClientAvailable(Context context, String packageName) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals(packageName)) {
                    return true;
                }
            }
        }
        return false;
    }
}
